//
//  LoadingListVC.m
//  CJRailTransport
//
//  Created by Hank on 2018/2/23.
//  Copyright © 2018年 amplity. All rights reserved.
//

#import "LoadingListVC.h"
#import "LoadingListCell.h"
#import "AddContactVC.h"
#import "AddContactModel.h"

@interface LoadingListVC ()<JSTokenFieldDelegate>

@end

@implementation LoadingListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置导航栏
    [self setNavC];
    // 设置界面属性
    [self setViewAttribute];
    // 注册Cell
    [self registerCells];
}

#pragma mark - 导航栏
- (void)setNavC
{
    self.title = @"装车清单";
    // 设置导航栏颜色
    BasicNavigationController *nVC = (BasicNavigationController *)self.navigationController;
    nVC.themeColor = [UIColor whiteColor];
    nVC.tintColor = [UIColor darkGrayColor];
}

// 设置界面属性
- (void)setViewAttribute
{
    self.view.backgroundColor = [UIColor whiteColor];
    // 设置发送邮箱
    self.sendEmail.backgroundColor = [UIColor clearColor];
    self.sendEmail.textField.tag = 0;
    [self.sendEmail setDelegate:self];
    
    // 设置抄送邮箱
    self.carbonCopyEmail.backgroundColor = [UIColor clearColor];
    self.carbonCopyEmail.textField.tag = 1;
    [self.carbonCopyEmail setDelegate:self];
}

#pragma mark - initArray
// 初始化数组
- (NSMutableArray *)receiptDataArray
{
    if (!_receiptDataArray) {
        _receiptDataArray = [[NSMutableArray alloc]init];
    }
    return _receiptDataArray;
}

- (NSMutableArray *)carbonCopyDataArray
{
    if (!_carbonCopyDataArray) {
        _carbonCopyDataArray = [[NSMutableArray alloc]init];
    }
    return _carbonCopyDataArray;
}

// 注册Cell
- (void)registerCells
{
    UINib *nib = [UINib nibWithNibName:[LoadingListCell reuseIdentifier] bundle:nil];
    [_myTableView registerNib:nib forCellReuseIdentifier:[LoadingListCell reuseIdentifier]];
}

#pragma mark - Action
// 添加联系人
- (IBAction)addBtnAction:(UIButton *)sender
{
    // 防止循环引用
    WEAK_REF(self);
    
    AddContactVC *addContactVC = [[AddContactVC alloc]init];
    addContactVC.stateTag = sender.tag;
    // 判断发送和抄送数据
    if (sender.tag == 0) {
        addContactVC.selectDataArray = [NSMutableArray arrayWithArray:self.receiptDataArray];
    }else{
        addContactVC.selectDataArray = [NSMutableArray arrayWithArray:self.carbonCopyDataArray];
    }
    // 复选框点击 Block
    [addContactVC setCheckImageClickBlock:^(AddContactModel *content, BOOL state, NSInteger stateTag){
        //======修改Model 自定义字段======
        if (state) {
            if (sender.tag == 0) {
                // 增加指定元素
                [weak_self.receiptDataArray addObject:content];
                // 添加发送人员
                [weak_self.sendEmail addTokenWithTitle:content.name representedObject:content];
            }else{
                [weak_self.carbonCopyDataArray addObject:content];
                // 添加抄送人员
                [weak_self.carbonCopyEmail addTokenWithTitle:content.name representedObject:content];
            }
        }else{
            if (sender.tag == 0) {
                // 移除指定元素
                [weak_self.receiptDataArray removeObject:content];
                // 移除发送人员
                [weak_self.sendEmail removeTokenWithRepresentedObject:content];
            }else{
                // 移除指定元素
                [weak_self.carbonCopyDataArray removeObject:content];
                // 移除抄送人员
                [weak_self.carbonCopyEmail removeTokenWithRepresentedObject:content];
            }
        }
        // 刷新Cell
//        [weak_self.myTableView reloadData];
    }];
    [self.navigationController pushViewController:addContactVC animated:YES];
}

#pragma mark - JSTokenFieldDelegate
- (void)tokenField:(JSTokenField *)tokenField height:(CGFloat)height
{
    if (tokenField.textField.tag == 0) {
        self.sendEmailH.constant = height + (self.receiptDataArray.count > 0 ? 10 : 0);
    }else{
        self.carbonCopyEmailH.constant = height + (self.carbonCopyDataArray.count > 0 ? 10 : 0);
    }
}

- (void)tokenField:(JSTokenField *)tokenField didAddToken:(NSString *)title representedObject:(id)obj
{
    if ([title isEqualToString:obj]) {
        AddContactModel *content = [[AddContactModel alloc]init];
        content.name = title;
        content.email = title;
        if (tokenField.textField.tag == 0) {
            [self.receiptDataArray addObject:content];
        }else{
            [self.carbonCopyDataArray addObject:content];
        }
    }
}

- (void)tokenField:(JSTokenField *)tokenField didRemoveToken:(NSString *)title representedObject:(id)obj
{
    if (tokenField.textField.tag == 0) {
        [self.receiptDataArray removeObject:obj];
    }else{
        [self.carbonCopyDataArray removeObject:obj];
    }
}

- (BOOL)tokenFieldShouldReturn:(JSTokenField *)tokenField {
    NSMutableString *recipient = [NSMutableString string];
    
    NSMutableCharacterSet *charSet = [[NSCharacterSet whitespaceCharacterSet] mutableCopy];
    [charSet formUnionWithCharacterSet:[NSCharacterSet punctuationCharacterSet]];
    
    NSString *rawStr = [[tokenField textField] text];
    for (int i = 0; i < [rawStr length]; i++)
    {
        if (![charSet characterIsMember:[rawStr characterAtIndex:i]])
        {
            [recipient appendFormat:@"%@",[NSString stringWithFormat:@"%c", [rawStr characterAtIndex:i]]];
        }
    }
    
    if ([rawStr length])
    {
        [tokenField addTokenWithTitle:rawStr representedObject:recipient];
    }
    
    [[tokenField textField] setText:@""];
    
    return NO;
}
#pragma mark - TableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 防止循环引用
    WEAK_REF(self);
    
    LoadingListCell *cell = [tableView dequeueReusableCellWithIdentifier:[LoadingListCell reuseIdentifier] forIndexPath:indexPath];
    cell.indexPath = indexPath;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
